package cn.wolfcode.wolf2w.Listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelUserIdFavors;
import cn.wolfcode.wolf2w.domain.UserIdFavors;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.ITravelStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.ITravelUserIdFavorsRedisService;
import cn.wolfcode.wolf2w.redis.service.IUserIdFavorsRedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.redis.vo.TravelStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.ITravelUserIdFavorsService;
import cn.wolfcode.wolf2w.service.IUserIdFavorsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisDaraInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    @Autowired
    private IUserIdFavorsService userIdFavorsService;
    @Autowired
    private IUserIdFavorsRedisService userIdFavorsRedisService;
    @Autowired
    private ITravelStatisVORedisService travelStatisVORedisService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelUserIdFavorsService travelUserIdFavorsService;
    @Autowired
    private ITravelUserIdFavorsRedisService travelUserIdFavorsRedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //初始化所有攻略
        List<Strategy> strategys = strategyService.findAll();
        for (Strategy strategy : strategys) {
            if (strategyStatisVORedisService.isStrategyVoExist(strategy.getId())){
                continue;
            }
            StrategyStatisVO strategyStatisVO = new StrategyStatisVO();
            BeanUtils.copyProperties(strategy, strategyStatisVO);
            strategyStatisVO.setStrategyId(strategy.getId());

            strategyStatisVORedisService.setStrategyStatisVO(strategyStatisVO);
        }

        //初始化所有游记
        List<Travel> travels = travelService.findAll();
        for (Travel travel : travels) {
            if (travelStatisVORedisService.isTravelVoExist(travel.getId())){
                continue;
            }
            TravelStatisVO travelStatisVO = new TravelStatisVO();
            BeanUtils.copyProperties(travel, travelStatisVO);
            travelStatisVO.setTravelId(travel.getId());

            travelStatisVORedisService.setTravelStatisVO(travelStatisVO);
        }

        //初始化我们的攻略用户列表
        List<UserIdFavors> userIdFavorsList = userIdFavorsService.findAll();
        for (UserIdFavors userIdFavors : userIdFavorsList) {
            if (userIdFavorsRedisService.isUserIdFavorsExist(userIdFavors.getUsid())){
                continue;
            }
            userIdFavorsRedisService.setUserIdFavorsRedis(userIdFavors);
        }

        //初始化游记用户列表
        List<TravelUserIdFavors> travelUserIdFavorsList = travelUserIdFavorsService.findAll();
        for (TravelUserIdFavors travelUserIdFavors : travelUserIdFavorsList) {
            if (travelUserIdFavorsRedisService.isTravelUserIdFavorsExist(travelUserIdFavors.getUsid())){
                continue;
            }
            travelUserIdFavorsRedisService.setTravelUserIdFavorsRedis(travelUserIdFavors);
        }
    }
}
